#include "../include/widget.h"
#include "../Ui_set/ui_widget.h"
#include<QMouseEvent>
#include<QMargins>


int widget::opacity() const
{
    return m_opacity;
}

void widget::setOpacity(int opacity)
{
    m_opacity=opacity;
    QString qss="background-color: rgba(225, 225, 225, %1);";
    ui->sideBarButton->setStyleSheet(qss.arg(m_opacity));
}

int widget::spacerLen() const
{
    return m_marginLen;
}

void widget::setSpacerLen(int len)
{
    m_marginLen=len;
    ui->horizontalLayout_6->setContentsMargins(0,0,m_marginLen,0);
}

QSize widget::thisSize() const
{
    return m_thisSize;
}

void widget::setThisSize(const QSize &size)
{
    m_thisSize=size;
    this->resize(m_thisSize);
}

int widget::statusLen() const
{
    return m_statusLen;
}

void widget::setStatusLen(const int len)
{
    m_statusLen=len;
    ui->horizontalLayout_5->setContentsMargins(0,0,len,0);
}

void widget::showDetailWidget(bool ok)
{
    if(m_mainWidgetAnim->animationCount()!=0)   m_mainWidgetAnim->clear();
    QPropertyAnimation *thisWidgetAnim=new QPropertyAnimation(this,"thisSize");
    QPropertyAnimation *statusLayoutAnim=new QPropertyAnimation(this,"statusLen");

    thisWidgetAnim->setStartValue(QSize(666,542));
    thisWidgetAnim->setEndValue(QSize(984,542));

    statusLayoutAnim->setStartValue(319);
    statusLayoutAnim->setEndValue(0);

    m_mainWidgetAnim->addAnimation(thisWidgetAnim);
    m_mainWidgetAnim->addAnimation(statusLayoutAnim);

    if(isCLickSideBar){
        m_mainWidgetAnim->setCurrentTime(0);
        m_mainWidgetAnim->setDirection(QParallelAnimationGroup::Forward);
        m_mainWidgetAnim->start();
    }
    else{
        m_mainWidgetAnim->setCurrentTime(m_mainWidgetAnim->duration());
        m_mainWidgetAnim->setDirection(QParallelAnimationGroup::Backward);
        m_mainWidgetAnim->start();
    }

    //qDebug()<<this->children().size()<<":"<<sizeof(*this);
    isCLickSideBar?ui->sideBarButton->setText("<<"):ui->sideBarButton->setText(">>");
    isCLickSideBar=!isCLickSideBar;
}

widget::widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::widget)
{
    ui->setupUi(this);
    this->setWindowTitle("");
    this->setSizePolicy(QSizePolicy::Ignored,QSizePolicy::Fixed);
    isSideBarIn=false;
    isCLickSideBar=true;
    m_sideBarAnimGrounp=new QParallelAnimationGroup(this);
    m_sideBarAnimGrounp=this->sideBarAnimGrounp();
    m_mainWidgetAnim=new QParallelAnimationGroup(this);

    ui->sideBarWidget->setMask(QRegion(0,0,ui->sideBarWidget->width()/2,ui->sideBarWidget->height()));
    ui->sendBtn_3->setShortcut(Qt::Key_Enter);
    ui->msgBrowser->viewport()->setMouseTracking(true);
    ui->msgBrowser->viewport()->installEventFilter(this);
    connect(ui->sideBarButton,SIGNAL(clicked(bool)),this,SLOT(showDetailWidget(bool)));
}

widget::~widget()
{
    delete ui;
}

QParallelAnimationGroup * widget::sideBarAnimGrounp()
{
    //初始信息
    QPoint cursideBarPos(22,0);

    QPropertyAnimation *sideBarButtonPosAnim=new QPropertyAnimation(ui->sideBarButton,"pos");
    QPropertyAnimation *sideBarButtonOpacityAnim=new QPropertyAnimation(this,"opacity");
    QPropertyAnimation *toolBarSize=new QPropertyAnimation(this,"spacerLen");

    sideBarButtonPosAnim->setStartValue(cursideBarPos);
    sideBarButtonPosAnim->setEndValue(QPoint(cursideBarPos.x()-21,cursideBarPos.y()));

    sideBarButtonOpacityAnim->setStartValue(0);
    sideBarButtonOpacityAnim->setEndValue(100);

    toolBarSize->setStartValue(0);
    toolBarSize->setEndValue(22);


    m_sideBarAnimGrounp->addAnimation(sideBarButtonPosAnim);
    m_sideBarAnimGrounp->addAnimation(sideBarButtonOpacityAnim);
    m_sideBarAnimGrounp->addAnimation(toolBarSize);

    return m_sideBarAnimGrounp;
}

bool widget::eventFilter(QObject *watched, QEvent *event)
{
    int restrictPosXLeft=ui->msgBrowser->x()+ui->msgBrowser->width()-40;
    int restrictPosXRight=ui->msgBrowser->x()+ui->msgBrowser->width();

    auto e=dynamic_cast<QMouseEvent *>(event);

    if(watched==ui->msgBrowser->viewport() && event->type()==QEvent::MouseMove){
        if(restrictPosXLeft<e->pos().x() && e->pos().x()<restrictPosXRight && !isSideBarIn){
            //qDebug()<<restrictPosXLeft<<":-"<<event->pos().x()<<"-:"<<restrictPosXRight;
            m_sideBarAnimGrounp->setCurrentTime(0);
            m_sideBarAnimGrounp->setDirection(QParallelAnimationGroup::Forward);
            m_sideBarAnimGrounp->start(QAbstractAnimation::KeepWhenStopped);
            isSideBarIn=true;
        }
        if((restrictPosXLeft>e->pos().x() || e->pos().x()>restrictPosXRight) && isSideBarIn){
            //qDebug()<<e->pos().x();
            m_sideBarAnimGrounp->setCurrentTime(m_sideBarAnimGrounp->duration());
            m_sideBarAnimGrounp->setDirection(QParallelAnimationGroup::Backward);
            m_sideBarAnimGrounp->start(QAbstractAnimation::KeepWhenStopped);
            isSideBarIn=false;
        }
    }
    return QWidget::eventFilter(watched,event);
}
